package com.whity.towerdefense.controler.states;

import com.whity.towerdefense.Utils;
import com.whity.towerdefense.controler.GameControler;
import com.whity.towerdefense.controler.I_State;
import com.whity.towerdefense.model.GameModel;
import com.whity.towerdefense.view.GraphicElt;

import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;

public class DragMapAP implements I_State
{
	// =============================================================
	// Private attributs :
	
	private PointF		m_LastPosition;
	private GraphicElt	m_MapGraphicElt;
	
	// =============================================================
	// Public functions :
	
	public DragMapAP(float p_StartingX, float p_StartingY)
	{
		m_LastPosition = new PointF(p_StartingX, p_StartingY);
		m_MapGraphicElt = GameModel.LEVEL.GetMap().GetGraphic();
	}
	
	public void OnChangeState()
	{
		
	}
	
	public void OnTouchEvent(MotionEvent p_Evt)
	{
		if(p_Evt.getAction() == MotionEvent.ACTION_MOVE)
		{
			float x = p_Evt.getX();
			float y = p_Evt.getY();
			PointF old_pos = m_MapGraphicElt.GetPosition();
			// float deltaX = (x - m_LastPosition.x);
			// float deltaY = (m_LastPosition.y - y);
			
			m_MapGraphicElt.SetPosition(old_pos.x + ((x - m_LastPosition.x))/Utils.RESOLUTION_X*Utils.SCREEN_RATIO, old_pos.y + ((m_LastPosition.y - y))/Utils.RESOLUTION_Y);
			
			m_LastPosition.x = x;
			m_LastPosition.y = y;
		}
		else if(p_Evt.getAction() == MotionEvent.ACTION_UP)
		{
			GameControler.SetState(new IdleAP());
		}
		else if(p_Evt.getAction() == MotionEvent.ACTION_POINTER_2_DOWN)
		{
			PointF pos = new PointF(p_Evt.getX(1), p_Evt.getY(1));
			GameControler.SetState(new ScaleAP(pos, m_LastPosition));
		}
		else if(p_Evt.getAction() == MotionEvent.ACTION_POINTER_1_DOWN)
		{
			PointF pos = new PointF(p_Evt.getX(), p_Evt.getY());
			GameControler.SetState(new ScaleAP(pos, m_LastPosition));
		}
	}

	public void OnClickEvent(View p_View)
	{
		
	}
	
	public void OnDrawerOpenEvent()
	{
		
	}
	
	public void OnDrawerCloseEvent()
	{
		
	}
	
	public void OnPause()
	{
		
	}
	
	public void OnResume()
	{
		
	}
	
	public boolean OnMotionEvent(View p_View, MotionEvent p_Evt)
	{
		if(p_Evt.getAction() == MotionEvent.ACTION_MOVE)
		{
			float x = p_Evt.getX();
			float y = p_Evt.getY();
			PointF old_pos = m_MapGraphicElt.GetPosition();
			// float deltaX = (x - m_LastPosition.x);
			// float deltaY = (m_LastPosition.y - y);
			
			m_MapGraphicElt.SetPosition(old_pos.x + ((x - m_LastPosition.x))/Utils.RESOLUTION_X*Utils.SCREEN_RATIO, old_pos.y + ((m_LastPosition.y - y))/Utils.RESOLUTION_Y);
			
			m_LastPosition.x = x;
			m_LastPosition.y = y;
			return true;
		}
		else if(p_Evt.getAction() == MotionEvent.ACTION_UP)
		{
			GameControler.SetState(new IdleAP());
			return true;
		}
		else if(p_Evt.getAction() == MotionEvent.ACTION_POINTER_2_DOWN)
		{
			PointF pos = new PointF(p_Evt.getX(1), p_Evt.getY(1));
			GameControler.SetState(new ScaleAP(pos, m_LastPosition));
			return true;
		}
		else if(p_Evt.getAction() == MotionEvent.ACTION_POINTER_1_DOWN)
		{
			PointF pos = new PointF(p_Evt.getX(), p_Evt.getY());
			GameControler.SetState(new ScaleAP(pos, m_LastPosition));
			return true;
		}
		return false;
	}
	
	// =============================================================
}
